package com.example.springboot_08_restful.controller;

import com.example.springboot_08_restful.pojo.BaseResult;
import com.example.springboot_08_restful.pojo.Message;
import com.example.springboot_08_restful.service.IMessageService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther cangshufeilun
 * @date 2022/4/3 15:14
 */
@Api(value = "message",description = "message api",position = 100,protocols = "http")
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private IMessageService imessageService;





    @GetMapping("/")
    public List<Message> list()
    {
        return imessageService.findAll();

    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查找消息", notes = "根据编号查找消息")
    public Message findOne(@PathVariable("id") Long id)
    {
        return imessageService.findById(id);
    }

    @ApiOperation(value = "添加消息", notes = "根据参数创建消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "消息 ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "text", value = "正文", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "摘要", required = false, dataType = "String", paramType = "query"),
    })

    @PostMapping("/")
    public  Message add(@RequestBody Message m)
    {
        this.imessageService.save(m);
        return m;
    }


    @DeleteMapping(value = "message/{id}")
    public void delete(@PathVariable("id") Long id) {
        this.imessageService.removeById(id);
    }


    @ApiOperation(value = "修改消息", notes = "根据参数修改消息")
    @PutMapping(value = "message")
    @ApiResponses({
            @ApiResponse(code = 100, message = "请求参数有误"),
            @ApiResponse(code = 101, message = "未授权"),
            @ApiResponse(code = 103, message = "禁止访问"),
            @ApiResponse(code = 104, message = "请求路径不存在"),
            @ApiResponse(code = 200, message = "服务器内部错误")
    })

    public Message modify(Message message) {
        this.imessageService.updateById(message);
        return message;
    }


    @PatchMapping(value="/message/text")
    public Message patch(Message message) {
        this.imessageService.updateText(message);
        return message;
    }

    @PatchMapping(value="/message/text")
    public BaseResult<Message> patch1(Message message) {
        this.imessageService.updateText(message);
        return BaseResult.success(message);
    }
}
